package com.zygh.policeasset.controller;

import cn.hutool.core.util.StrUtil;
import com.zygh.policeasset.component.CommonResult;
import com.zygh.policeasset.pojo.Contract;
import com.zygh.policeasset.service.ContractService;
import com.zygh.policeasset.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping({"/contract"})
@Api(tags = {"合同管理模块"})
@RestController
public class ContractController {
    @Autowired
    ContractService contractService;
    @Autowired
    RoleService roleService;

    @RequestMapping(value = {"/"}, method = {RequestMethod.GET})
    @ApiOperation("获取合同分页列表")
    public Object findAll(@RequestParam String name,
                          @RequestParam(value = "status", required = false) Integer status,
                          @RequestParam(value = "companyId", required = false) Integer companyId,
                          @RequestParam(value = "pageNum", required = false) Integer pageNum,
                          @RequestParam(value = "pageSize", required = false) Integer pageSize,
                          @RequestParam(required = false) String time1,
                          @RequestParam(required = false) String time2,
                          @RequestParam(required = false) Integer category,
                          @RequestHeader("findAll") boolean findAll,
                          @RequestHeader("userId") Integer userId) {
        if (findAll) {
            return this.contractService.findAll(name, status, companyId, pageNum, pageSize, time1, time2, category, null);
        }
        return this.contractService.findAll(name, status, companyId, pageNum, pageSize, time1, time2, category, userId);
    }

    @RequestMapping(value = {"/findcontractpay"}, method = {RequestMethod.GET})
    @ApiOperation("获取合同分页列表")
    public Object findcontractpay(@RequestParam String name,
                                  @RequestParam(value = "status", required = false) Integer status,
                                  @RequestParam(value = "companyId", required = false) Integer companyId,
                                  @RequestParam(defaultValue = "1", value = "pageNum") Integer pageNum,
                                  @RequestParam(defaultValue = "10", value = "pageSize") Integer pageSize,
                                  @RequestParam(required = false) String time1,
                                  @RequestParam(required = false) String time2,
                                  @RequestParam(required = false) Integer category) {
        return this.contractService.findcontractpay(name, status, companyId, pageNum, pageSize, time1, time2, category);
    }

    @RequestMapping(value = {"/status"}, method = {RequestMethod.GET})
    @ApiImplicitParams({@ApiImplicitParam(name = "status", value = "0 未提交 1 已提交 2通过 3未通过", required = true, paramType = "query"), @ApiImplicitParam(name = "pageNum", value = "页码", required = true, paramType = "query"), @ApiImplicitParam(name = "pageSize", value = "页数", required = true, paramType = "query")})
    @ApiOperation("根据状态获取合同分页列表")
    public Object getByStatus(@RequestParam int status, @RequestParam(defaultValue = "1", value = "pageNum") int pageNum, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize) {
        if (StrUtil.isEmpty(String.valueOf(status))) {
            return new CommonResult().validateFailed("状态不能为空");
        }
        return this.contractService.findByStatus(status, pageNum, pageSize);
    }

    @RequestMapping(value = {"/list"}, method = {RequestMethod.GET})
    @ApiOperation("根据项目获取合同分页列表")
    public Object get(@RequestParam String projectNo, @RequestParam(defaultValue = "1", value = "pageNum") int pageNum, @RequestParam(defaultValue = "10", value = "pageSize") int pageSize) {
        if (StrUtil.isEmpty(projectNo)) {
            return new CommonResult().validateFailed("项目编号不能为空");
        }
        return this.contractService.findByProject(projectNo, pageNum, pageSize);
    }

    @RequestMapping(value = {"/getproject"}, method = {RequestMethod.GET})
    @ApiOperation("获取可添加项目")
    public Object getProject() {
        return this.contractService.getProject();
    }

    @RequestMapping(value = {"/getadd"}, method = {RequestMethod.GET})
    @ApiOperation("获取可添加标段")
    public Object getAdd(@RequestParam String projectNo) {
        if (StrUtil.isEmpty(projectNo)) {
            return new CommonResult().validateFailed("项目编号不能为空");
        }
        return this.contractService.getAdd(projectNo);
    }

    @RequestMapping(value = {"/add"}, method = {RequestMethod.POST})
    @ApiOperation("添加合同")
    public Object add(@RequestBody Contract contract, @RequestHeader("roleId") Integer roleId) {
        if (StrUtil.isEmpty(contract.getProjectNo())) {
            return new CommonResult().validateFailed("项目编号不能为空");
        }
        if (StrUtil.isEmpty(contract.getProjectName())) {
            return new CommonResult().validateFailed("项目名称不能为空");
        }
        if (StrUtil.isEmpty(String.valueOf(contract.getSectionId()))) {
            return new CommonResult().validateFailed("标段id不能为空");
        }
        if (this.roleService.findRoleById(roleId.intValue()).getContractEntry() == 0 && this.contractService.checkTime(contract.getConfirmTime())) {
            return new CommonResult().validateFailed("当前时间离合同签订日期已超过30天，无法录入，请联系支队财务");
        }
        if (this.contractService.add(contract).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("添加错误");
    }

    @RequestMapping(value = {"/update"}, method = {RequestMethod.POST})
    @ApiOperation("修改合同")
    public Object update(@RequestBody Contract params, HttpServletRequest request) {
        if (StrUtil.isEmpty(String.valueOf(params.getId()))) {
            return new CommonResult().validateFailed("ID不能为空");
        }
        if (this.roleService.findRoleById(Integer.valueOf(Integer.parseInt(request.getHeader("roleId"))).intValue()).getContractEntry() == 0 && this.contractService.checkTime(params.getConfirmTime())) {
            return new CommonResult().validateFailed("当前时间离合同签订日期已超过30天，无法录入，请联系支队财务");
        }
        if (this.contractService.update(params).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("修改错误");
    }

    @RequestMapping(value = {"/updatepay"}, method = {RequestMethod.POST})
    @ApiOperation("修改合同付款")
    public Object updatepay(@RequestBody Contract params) {
        if (StrUtil.isEmpty(String.valueOf(params.getId()))) {
            return new CommonResult().validateFailed("ID不能为空");
        }
        if (this.contractService.updatePay(params).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("修改错误");
    }

    @DeleteMapping({"/remove/{id}"})
    @ApiOperation("删除合同")
    public Object remove(@PathVariable Integer id) {
        if (StrUtil.isEmpty(String.valueOf(id))) {
            return new CommonResult().validateFailed("id不能为空");
        }
        if (this.contractService.remove(id.intValue()).intValue() > 0) {
            return new CommonResult().success();
        }
        return new CommonResult().validateFailed("删除错误");
    }

    @RequestMapping(value = {"/findById"}, method = {RequestMethod.GET})
    @ApiOperation("按ID查询合同信息")
    public Object findById(@RequestParam int id) {
        return new CommonResult().success(this.contractService.findById(Integer.valueOf(id)));
    }
}